import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import React, { useState, useRef, forwardRef, useImperativeHandle, useCallback, } from "react";
// import DevPopup from "../../components-ui/express-components/DevPopup";
import { DevPopup } from "~/public/cpas-ui";
import { CheckBox, LoadIndicator, TextBox, TreeList, } from "devextreme-react";
import AntTabs, { HocVerticalLeftStyleTab, } from "~/public/cpas-ui/components-ui/ant-components/AntTab";
import { AssistAdd, AssistDel, AssistEdit, AssistgetType, AutoAddChildrenForFZZ, getAccountInfo, getNewKMBH, getSubjectParentList, getSubjectSDB, getSubjectType, IsCanAddFZZ, SubjectIsHasFZZ, } from "./DraftApi";
import { Column } from "devextreme-react/tree-list";
import "./_style.less";
import notify from "devextreme/ui/notify";
import { CpasIcon } from "~/public/cpas-ui";
import { moneyFormat } from "./components";
import SubjectsPopupData from "../components/SubjectsPopupData";
import { Addsubject, Editsubject, SubjectDel } from "../SubjectPopup/DraftApi";
import AuxiliaryPopup from "../components/AuxiliaryPopup";
import { nanoid10 } from "../../tools/Tools";
/**
 * ## 基于本地数据库，获取数据本地数据进行功能演示。
 * ~~~js
 * import SubjectPopup  from 'sright-ui'
 * ~~~
 */
export const CarryEntryPopup = forwardRef((props, ref) => {
    const [visible, setVisible] = useState(false);
    const [tabsData, setTabsData] = useState([]);
    const [SubjectSDBData, setSubjectSDBData] = useState([]);
    const [SubjectSDBDatas, setSubjectSDBDatas] = useState([]);
    const [selectMode, setSelectMode] = useState("");
    const [assistType, setAssistType] = useState("");
    const [AddFZZMessage, setAddFZZMessage] = useState("");
    const [value, setValue] = useState("");
    const [TbsStates, setTbsStates] = useState("0");
    const [AddFZZKMMC, setIsCanAddFZZKMMC] = useState("");
    const [CheckBoxVaue, setCheckBoxVaue] = useState(true);
    const [openVisible111111, setOpenVisible111111] = useState(true);
    const [curRowKMMC, setCurRowKMMC] = useState("");
    const [treeListKey, setTreeListKey] = useState("");
    const diaParams = useRef();
    const diaParamsData = useRef();
    const TreeListRef = useRef();
    const typeRef = useRef();
    const AuxiliaryRef = useRef();
    const AuxiliaryRefs = useRef();
    // const ParentLevel = useRef<any>();
    const curNodeRef = useRef();
    // 删除科目
    const DeleteSubjectRef = useRef();
    // 增加科目
    const SubjectPopupRef = useRef();
    // 编辑科目
    // const EditSubjectsReft = useRef<any>();
    // 辅助账
    const AuxiliaryPopupRef = useRef();
    const TreeAuxiliaryRef = useRef();
    const typeErrorRef = useRef();
    // 获取切换数据
    const getSubjectTypeData = async () => {
        // setOpenVisible111111(true);
        const res = await getSubjectType({
            dbname: diaParams.current?.dbname,
            Data_ID: diaParams.current?.Data_ID,
        });
        if (res) {
            res.unshift({ code: "0", type: "全部" });
            setTabsData(res);
        }
        else {
            setTabsData([]);
        }
        // setTimeout(() => {
        //   setOpenVisible111111(false);
        // }, 1000);
    };
    // 获取表格数据
    const getSubjectSDBDatas = async (CheckBoxVaue) => {
        setOpenVisible111111(true);
        const res = await getSubjectSDB({
            dbname: diaParams.current?.dbname,
            Data_ID: diaParams.current?.Data_ID,
            isHasFZZ: CheckBoxVaue,
        });
        setTreeListKey(nanoid10());
        diaParamsData.current = {};
        if (res.success) {
            setSubjectSDBData(res.data);
            setSubjectSDBDatas(res.data);
            setOpenVisible111111(false);
        }
        else {
            setSubjectSDBData([]);
            setSubjectSDBDatas([]);
            setOpenVisible111111(false);
        }
    };
    // 自定义方法
    useImperativeHandle(ref, () => ({
        show: async (params) => {
            diaParams.current = params;
            setVisible(true);
            setSelectMode(diaParams.current?.selectMode);
            getSubjectTypeData();
            setCheckBoxVaue(diaParams.current?.isHasFZZ);
            getSubjectSDBDatas(diaParams.current?.isHasFZZ);
        },
    }));
    // useEffect(() => {
    //   getSubjectSDBDatas();
    // }, [CheckBoxVaue]);
    const reloadTableAndTas = () => {
        getSubjectSDBDatas(CheckBoxVaue);
        setTbsStates("0");
        TreeListRef.current.instance.searchByText();
        TreeListRef.current.instance.clearSelection();
    };
    // 显示总账不对
    // 模糊搜索
    const inputSearch = (content) => {
        TreeListRef.current.instance.searchByText(content || "");
    };
    // const throttleSearch = throttle(inputSearch, 100);
    const getInputValue = (e) => {
        const { event } = e;
        inputSearch(event.target.value);
        setValue(event.target.value);
    };
    const getSubjectListData = async (r) => {
        setTreeListKey(nanoid10());
        diaParamsData.current = {};
        if (r === "全部") {
            if (SubjectSDBData.length > 0)
                setSubjectSDBDatas(SubjectSDBData);
        }
        else {
            if (SubjectSDBData.length > 0) {
                const KMLXData = SubjectSDBData.filter((item) => {
                    return item.KMLX && item.KMLX.includes(r);
                });
                // console.log(KMLXData);
                setSubjectSDBDatas(KMLXData);
            }
        }
    };
    const AccountTabsClick = useCallback(async (r) => {
        setTbsStates(r);
        if (tabsData) {
            tabsData.map(async (item) => {
                if (r === item.code) {
                    getSubjectListData(item.type);
                }
                return "";
            });
        }
    }, [tabsData, SubjectSDBData]);
    function handleStr(str) {
        var arr = str.replace(/[`:_.~!@#$%^&*() \+ =<>?"{}|, \/ ;' \\ [ \] ·~！@#￥%……&*（）—— \+ ={}|《》？：“”【】、；‘’，。、]/g, "");
        return arr;
    }
    const saveAddsubject = async (tempObj, isAddMoreYear) => {
        const { dbname, Data_ID, editUser } = diaParams.current;
        if (!isAddMoreYear) {
            const res = await Addsubject(tempObj);
            if (res.ok) {
                notify("增加科目成功", "success", 2000);
                getSubjectSDBDatas(CheckBoxVaue);
            }
            return;
        }
        const resYear = await getAccountInfo({ dbname });
        if (resYear.success) {
            const yearData = resYear.data.map((item) => item.Data_ID);
            const tempA = [];
            for (let index = 0; index < yearData.length; index++) {
                const element = yearData[index];
                if (element) {
                    const res = await Addsubject({ ...tempObj, Data_ID: element });
                    if (res.ok && res.data > 0) {
                        tempA.push(Promise.resolve("ok"));
                    }
                    else {
                        tempA.push(Promise.resolve(`${element}年:${res.data}`));
                    }
                }
            }
            Promise.allSettled(tempA)
                .then((results) => {
                console.log(tempA, results, "@@@");
                reloadTableAndTas();
                const falseData = results
                    .filter((item1) => item1.value !== "ok")
                    .map((item) => item.value);
                if (falseData.length) {
                    // notify(`增加科目失败，失败原因为【${falseData.join(';')};】`, falseData.length === yearData.length ? 'error' : 'warning', 2000);
                    notify(`增加科目失败，失败原因为【${falseData.join(";")};】`, "warning", 4000);
                }
                else {
                    notify("增加科目成功", "success", 2000);
                }
            })
                .catch((error) => {
                // 处理错误
                console.error("An error occurred:", error);
            });
        }
        else {
            notify(`获取年度失败！`, "error", 2000);
        }
    };
    const saveAssistAdd = async (dataObj, isAddMoreYear) => {
        const { dbname, Data_ID, editUser } = diaParams.current;
        if (!isAddMoreYear) {
            const data = await AssistAdd(dataObj);
            if (data.ok) {
                notify("增加辅助账成功", "success", 2000);
                reloadTableAndTas();
            }
            else {
                notify("该科目下已存在相同名称的辅助核算项目，无法重复新增！", "error", 4000);
            }
            return;
        }
        const resYear = await getAccountInfo({ dbname });
        if (resYear.success) {
            const yearData = resYear.data.map((item) => item.Data_ID);
            const tempA = [];
            for (let index = 0; index < yearData.length; index++) {
                const element = yearData[index];
                if (element) {
                    const res = await AssistAdd({ ...dataObj, Data_ID: element });
                    if (res.ok && res.data > 0) {
                        tempA.push(Promise.resolve("ok"));
                    }
                    else {
                        tempA.push(Promise.resolve(`${element}年:${res.data}`));
                    }
                }
            }
            Promise.allSettled(tempA)
                .then((results) => {
                console.log(tempA, results, "@@@");
                reloadTableAndTas();
                const falseData = results
                    .filter((item1) => item1.value !== "ok")
                    .map((item) => item.value);
                if (falseData.length) {
                    // notify(`增加科目失败，失败原因为【${falseData.join(';')};】`, falseData.length === yearData.length ? 'error' : 'warning', 2000);
                    notify(`增加科目失败，失败原因为【${falseData.join(";")};】`, "warning", 2000);
                }
                else {
                    notify("增加辅助账成功", "success", 2000);
                }
            })
                .catch((error) => {
                // 处理错误
                console.error("An error occurred:", error);
            });
        }
        else {
            notify(`获取年度失败！`, "error", 2000);
        }
    };
    // 判断 是否为 66为辅助类型 99辅助账
    const subjectMethod = (e, r) => {
        const itemNames = r.selectedYears || "";
        let tempObj = {};
        if (e) {
            //r ? 有返回参数 说明他有上级科目 : 说明它是增加一级科目
            tempObj = {
                dbname: diaParams.current?.dbname,
                Data_ID: diaParams.current?.Data_ID,
                KMBH: r.KMBH ? r.KMBH : r.KMBHAdd + "~|",
                KMMC: r.KMMC ? r.KMMC + "-" + r.SubjectNumber : r.SubjectNumber,
                editUser: diaParams.current?.editUser,
                PKMBH: r.PKMBH,
                YSKMBH: `${r.KMBHAdd}`,
                YSKMMC: r.SubjectNumber,
                KMFX: r.SelectName,
                KMLX: r.SelectType,
                JC: r.Satatejc ? Number(r.Satatejc) + 1 : 1,
                itemNames: itemNames,
                checkBoxState: r.checkBoxState,
            };
            // console.log(tempObj, "@@@");
            saveAddsubject(tempObj, r.checkBoxState);
        }
    };
    const openAuxiliaryPopup = (tmepArgs, tmepBH, tempRowData) => {
        let tempObj = {
            dbname: diaParams.current?.dbname,
            Data_ID: diaParams.current?.Data_ID,
            editUser: diaParams.current?.editUser,
        };
        AuxiliaryPopupRef.current.show({
            args: tmepArgs,
            okCallback: (v) => {
                tempObj = {
                    ...tempObj,
                    KMBH: v.KMBH ? v.KMBH : tempRowData.KMBH,
                    KMMC: v.KMMC ? v.KMMC : tempRowData.KMMC,
                    HSXMLXBH: v.SelectHSXMLXBH,
                    YSKMBH: `${v.YSKMBH}` ? `${v.YSKMBH}` : `${tmepBH}`,
                    HSXMLXMC: v.selectValue,
                    HSXMBH: v.SelectHSXMLXBH + "-" + v.projectValueMds + "~|",
                    HSXMMC: v.projectValues,
                    YSHSXMMC: v.projectValues,
                    YSHSXMBH: v.SelectHSXMLXBH + "-" + v.projectValueMds,
                };
                saveAssistAdd(tempObj, v.checkBoxState);
            },
            cancelCallback: () => { },
        });
    };
    const menuAddsubjectFunc = async (rowData) => {
        const MatchKMMC = rowData.KMMC.match(/(\S*)-/);
        let tabsArray = [...tabsData];
        tabsArray.shift();
        if (rowData.JC === 1) {
            SubjectPopupRef.current.show({
                args: {
                    title: "新增科目",
                    KMBH: "",
                    data: tabsArray,
                    ParentLevel: "",
                    ParentLevelKmbh: "",
                    PKMBH: "",
                    resolvess: rowData.KMMC,
                    datatKMFX: rowData.KMFX,
                    dataKMLX: rowData.KMLX,
                    isEdit: false,
                },
                okCallback: (r) => {
                    // console.log(r, "@@@ 新增科目");
                    if (r) {
                        const itemNames = r.selectedYears || "";
                        let tempObj = {
                            dbname: diaParams.current?.dbname,
                            Data_ID: diaParams.current?.Data_ID,
                            editUser: diaParams.current?.editUser,
                            YSKMBH: `${r.KMBHAdd}`,
                            YSKMMC: r.SubjectNumber,
                            KMFX: r.SelectName,
                            KMLX: r.SelectType,
                            itemNames: itemNames,
                            KMBH: r.KMBH ? r.KMBH : r.KMBHAdd + "~|",
                        };
                        if (r.Satatejc) {
                            tempObj = {
                                ...tempObj,
                                KMMC: r.KMMC + "-" + r.SubjectNumber,
                                PKMBH: r.PKMBH,
                                JC: Number(r.Satatejc) + 1,
                            };
                        }
                        else {
                            tempObj = {
                                ...tempObj,
                                KMMC: r.SubjectNumber,
                                PKMBH: "",
                                JC: rowData.JC,
                            };
                        }
                        // console.log(r, tempObj, "@@@ 这是新增科目tempObj");
                        saveAddsubject(tempObj, r.checkBoxState);
                    }
                },
                cancelCallback: () => { },
            });
            return;
        }
        if (rowData.ZWType === 1) {
            // const eKMMC = curNodeRef.current.KMMC.match(/(\S*)-/);
            // 判断 是否为 66为辅助类型 99辅助账
            if (rowData.Level === 66) {
                // 在辅助类型上 增加 科目参数传递有误
                typeRef.current.show({
                    title: "提示",
                    okCallback: () => {
                        typeErrorRef.current.show({
                            title: "提示",
                            okCallback: () => {
                                // 后期在业务组件里面做判断
                                SubjectPopupRef.current.show({
                                    args: {
                                        title: "新增科目",
                                        KMBH: "",
                                        data: tabsArray,
                                        ParentLevel: "",
                                        ParentLevelKmbh: "",
                                        PKMBH: "",
                                        resolvess: "",
                                        datatKMFX: rowData.KMFX,
                                        dataKMLX: rowData.KMLX,
                                        isEdit: false,
                                    },
                                    okCallback: (r) => {
                                        subjectMethod(MatchKMMC, r);
                                    },
                                    cancelCallback: () => { },
                                });
                            },
                        });
                    },
                });
                return;
            }
            if (rowData.Level === 99) {
                // 在辅助账数据上 增加 科目参数传递有误
                SubjectPopupRef.current.show({
                    args: {
                        title: "新增科目",
                        KMBH: "",
                        data: tabsArray,
                        ParentLevel: "",
                        ParentLevelKmbh: "",
                        PKMBH: "",
                        resolvess: "",
                        datatKMFX: rowData.KMFX,
                        dataKMLX: rowData.KMLX,
                        isEdit: false,
                    },
                    okCallback: (r) => {
                        // console.log(r, "--rrr-----rrrr辅助帐增加科目");
                        subjectMethod(MatchKMMC, r);
                    },
                    cancelCallback: () => { },
                });
            }
            return;
        }
        // // console.log(rowData, "data----");
        let parentLevelName = "";
        let resolvess = "";
        if (rowData.PKMBH) {
            const res = await getSubjectParentList({
                dbname: diaParams.current?.dbname,
                Data_ID: diaParams.current?.Data_ID,
                PKMBH: rowData.PKMBH,
            });
            if (res.success) {
                const { data } = res;
                parentLevelName = data.displayName;
                resolvess = data.KMMC;
            }
        }
        const RNewKMBH = await getNewKMBH({
            dbname: diaParams.current?.dbname,
            Data_ID: diaParams.current?.Data_ID,
            KMBH: rowData.PID,
        });
        SubjectPopupRef.current.show({
            args: {
                title: "新增科目",
                KMBH: RNewKMBH.data[0].KMBH,
                data: tabsArray,
                ParentLevel: parentLevelName,
                ParentLevelKmbh: RNewKMBH.data[0].YSKMBH,
                PKMBH: rowData.PKMBH,
                resolvess,
                datatKMFX: rowData.KMFX,
                dataKMLX: rowData.KMLX,
                isEdit: false,
            },
            okCallback: (r) => {
                // console.log(r, "------");
                // 修改 变动
                const itemNames = r.selectedYears || "";
                let dataObj = {};
                // 如果 他的上级科目被删除了 执行下方方法
                if (r.PKMBHState) {
                    dataObj = {
                        dbname: diaParams.current?.dbname,
                        Data_ID: diaParams.current?.Data_ID,
                        editUser: diaParams.current?.editUser,
                        KMBH: r.KMBH ? r.KMBH : r.KMBHAdd + "~|",
                        KMMC: r.SubjectNumber,
                        PKMBH: "",
                        YSKMBH: `${r.KMBHAdd}`,
                        YSKMMC: r.SubjectNumber,
                        KMFX: r.SelectName,
                        KMLX: r.SelectType,
                        JC: 1,
                        itemNames: itemNames,
                    };
                }
                else {
                    dataObj = {
                        dbname: diaParams.current?.dbname,
                        Data_ID: diaParams.current?.Data_ID,
                        editUser: diaParams.current?.editUser,
                        KMBH: r.KMBH ? r.KMBH : RNewKMBH.data[0].KMBH,
                        KMMC: `${r.KMMC ? r.KMMC : resolvess}-${r.SubjectNumber}`,
                        PKMBH: r.PKMBH,
                        YSKMBH: `${r.KMBHAdd}`,
                        YSKMMC: r.SubjectNumber,
                        KMFX: r.SelectName,
                        KMLX: r.SelectType,
                        JC: r.Satatejc ? Number(r.Satatejc) + 1 : rowData.JC,
                        itemNames: itemNames,
                    };
                }
                saveAddsubject(dataObj, r.checkBoxState);
            },
            cancelCallback: () => { },
        });
    };
    // 判断是否可以增加辅助账
    const IsCanAddFZZStates = async (vData, isBoolean, vRowData) => {
        if (isBoolean) {
            const YSKMBH = handleStr(vRowData.KMBH);
            const r = await AssistgetType({
                dbname: diaParams.current?.dbname,
                Data_ID: diaParams.current?.Data_ID,
                KMBH: vRowData.KMBH,
            });
            openAuxiliaryPopup({
                title: "新增辅助账",
                value: YSKMBH + "-" + vRowData.KMMC,
                auxiliaryState: true,
                items: r,
                edit: false,
                fzzData: vRowData,
            }, YSKMBH, vRowData);
            return;
        }
        const value = await AutoAddChildrenForFZZ({
            dbname: diaParams.current?.dbname,
            Data_ID: diaParams.current?.Data_ID,
            editUser: diaParams.current?.editUser,
            KMBH: vRowData.KMBH,
        });
        const r = await AssistgetType({
            dbname: diaParams.current?.dbname,
            Data_ID: diaParams.current?.Data_ID,
            KMBH: value[0].KMBH,
        });
        const PKMBH = handleStr(value[0].KMBH);
        // console.log(value, "value---");
        if (vData.code === 1) {
            openAuxiliaryPopup({
                title: "新增辅助账",
                value: PKMBH + "-" + value[0].KMMC,
                auxiliaryState: true,
                items: r,
                edit: false,
                fzzData: vRowData,
            }, PKMBH, vRowData);
            return;
        }
        else if (vData.code === 2) {
            setIsCanAddFZZKMMC(vData.message);
            TreeAuxiliaryRef.current.show({
                title: "提示",
                okCallback: () => {
                    openAuxiliaryPopup({
                        title: "新增辅助账",
                        value: PKMBH + "-" + value[0].KMMC,
                        auxiliaryState: true,
                        items: r,
                        edit: false,
                        fzzData: value[0],
                    }, PKMBH, vRowData);
                },
            });
            return;
        }
    };
    const menuEditsubjectFunc = async (rowData) => {
        const YSKMBH = handleStr(rowData.KMBH);
        const YSHSXMBH = handleStr(rowData.code);
        let tabsArray = [...tabsData];
        tabsArray.shift();
        if (rowData.ZWType === 1) {
            const r = await AssistgetType({
                dbname: diaParams.current?.dbname,
                Data_ID: diaParams.current?.Data_ID,
                KMBH: rowData.KMBH,
            });
            // 判断 是否为 66为辅助类型 99辅助账
            if (rowData.level === 66) {
                return openAuxiliaryPopup({
                    title: "新增辅助账",
                    value: YSKMBH + "-" + rowData.KMMC,
                    auxiliaryState: true,
                    items: r,
                    edit: false,
                    fzzData: rowData,
                }, YSKMBH, rowData);
            }
            // console.log(rowData, "curNodeRef.current.KMBH---");
            if (rowData.isNewAdd) {
                return AuxiliaryPopupRef.current.show({
                    args: {
                        title: "编辑辅助账",
                        value: YSKMBH + "-" + rowData.KMMC,
                        auxiliaryState: true,
                        items: r,
                        edit: true,
                        HSXMMC: rowData.HSXMMC,
                        fzzData: rowData,
                    },
                    okCallback: async (v) => {
                        const data = await AssistEdit({
                            dbname: diaParams.current?.dbname,
                            Data_ID: diaParams.current?.Data_ID,
                            editUser: diaParams.current?.editUser,
                            KMBH: rowData.KMBH,
                            KMMC: rowData.KMMC,
                            HSXMLXBH: v.SelectHSXMLXBH,
                            YSKMBH: `${YSKMBH}`,
                            HSXMLXMC: v.selectValue,
                            HSXMBH: rowData.code + "~|",
                            HSXMMC: v.projectValue,
                            YSHSXMMC: v.projectValue,
                            YSHSXMBH: YSHSXMBH,
                        });
                        if (data) {
                            notify("修改辅助账成功", "success", 2000);
                            getSubjectSDBDatas(CheckBoxVaue);
                        }
                    },
                    cancelCallback: () => { },
                });
            }
            notify(`辅助账【${rowData.HSXMMC}】为企业原始辅助账,不可编辑`, "warning", 2000);
            return;
        }
        else {
            // console.log(rowData, "111111");
            // 编辑 科目
            if (rowData.isNewAdd) {
                // console.log(rowData, "@@@ 编辑 科目");
                let parentLevelName = "";
                let resolvess = "";
                if (rowData.PKMBH) {
                    const res = await getSubjectParentList({
                        dbname: diaParams.current?.dbname,
                        Data_ID: diaParams.current?.Data_ID,
                        PKMBH: rowData.PKMBH,
                    });
                    if (res.success) {
                        const { data } = res;
                        parentLevelName = data.displayName;
                        resolvess = data.KMMC;
                    }
                }
                SubjectPopupRef.current.show({
                    args: {
                        title: "编辑",
                        KMBH: rowData.KMBH,
                        data: tabsArray,
                        ParentLevel: rowData.JC === 1 ? "" : parentLevelName,
                        ParentLevelKmbh: Number(rowData?.YSKMBH || 0),
                        PKMBH: rowData.PKMBH,
                        KMMC: rowData.YSKMMC,
                        datatKMFX: rowData.KMFX,
                        dataKMLX: rowData.KMLX,
                        itemNames: rowData.itemNames,
                        resolvess,
                        isEdit: true,
                    },
                    okCallback: async (v) => {
                        // console.log(resolvess, "resolvess-----");
                        // console.log("确定回调函数");
                        const itemNames = v.selectedYears || rowData.itemNames;
                        const tempKMMC = `${resolvess && resolvess + "-"}${v.SubjectNumber}`;
                        const r = await Editsubject({
                            dbname: diaParams.current?.dbname,
                            Data_ID: diaParams.current?.Data_ID,
                            editUser: diaParams.current?.editUser,
                            KMBH: rowData.KMBH,
                            KMMC: tempKMMC,
                            YSKMMC: v.SubjectNumber,
                            KMFX: v.SelectName,
                            KMLX: v.SelectType,
                            itemNames: itemNames,
                        });
                        if (r.ok) {
                            notify("编辑科目成功", "success", 2000);
                            getSubjectSDBDatas(CheckBoxVaue);
                        }
                    },
                    cancelCallback: () => { },
                });
            }
            else {
                notify(`科目【${rowData.KMMC}】为企业原始科目,不可编辑`, "warning", 2000);
            }
        }
    };
    // const firstLevelSubjectFunc = () => {
    //   FirstLevelSubjectPopupRef.current.show({
    //     args: {
    //       title: '快速新增一级科目',
    //       selectYear: AccountDataID
    //     },
    //     okCallback: () => {
    //       getSubjectListDatas(AccountDataID);
    //     }
    //   })
    // }
    const relationMenuItems = (e) => {
        if (!TreeListRef.current)
            return;
        if (e?.target == "content" && e?.row?.rowType == "data") {
            // console.log(e, e.row?.data, "@@@");
            if (!e.items) {
                e.items = [];
            }
            const rowData = e.row?.data;
            curNodeRef.current = e.row?.data;
            TreeListRef.current?.instance.selectRows(e.row.key);
            setCurRowKMMC(rowData.KMMC);
            const tempMenus = [
                // {
                //   text: "快速新增一级科目",
                //   onItemClick: () => {
                //     firstLevelSubjectFunc();
                //   },
                // },
                {
                    text: "增加科目",
                    onItemClick: () => {
                        // console.log(rowData, "@@@ 增加科目");
                        // 判断 科目是否为一级
                        menuAddsubjectFunc(rowData);
                    },
                },
                {
                    text: "增加辅助账",
                    onItemClick: async () => {
                        const IsHasFZZ = await SubjectIsHasFZZ({
                            dbname: diaParams.current?.dbname,
                            Data_ID: diaParams.current?.Data_ID,
                            KMBH: rowData.KMBH,
                        });
                        const IsCanAddFZZState = await IsCanAddFZZ({
                            dbname: diaParams.current?.dbname,
                            Data_ID: diaParams.current?.Data_ID,
                            KMBH: rowData.KMBH,
                        });
                        // console.log(IsHasFZZ, "IsHasFZZ---");
                        // 判断 是否存在辅助账
                        if (IsHasFZZ[0].isExists) {
                            // 存在辅助账 判断是否可以增加辅助账
                            IsCanAddFZZStates(IsCanAddFZZState[0], true, rowData);
                        }
                        else {
                            if (IsCanAddFZZState[0].code === 0) {
                                // console.log("触发---");
                                notify(`${IsCanAddFZZState[0].message}`, "warning", 2000);
                                return;
                            }
                            IsCanAddFZZStates(IsCanAddFZZState[0], false, rowData);
                        }
                    },
                },
                {
                    text: "编辑",
                    onItemClick: () => {
                        // console.log(rowData, "curNodeRef.current--------");
                        menuEditsubjectFunc(rowData);
                    },
                },
                {
                    text: "删除",
                    onItemClick: () => {
                        // console.log(rowData, "@@@");
                        if (!rowData.isNewAdd) {
                            if (rowData.level === 99) {
                                return notify(`辅助账【${rowData.HSXMMC}】为企业原始辅助账,不可编辑`, "warning", 2000);
                            }
                            if (rowData.level === 33) {
                                return notify(`科目【${rowData.KMMC}】为企业原始科目,不可编辑`, "warning", 2000);
                            }
                        }
                        DeleteSubjectRef.current.show({
                            args: {
                                title: "询问",
                                codes: true,
                                Name: rowData.displayName,
                                HSXMLXBH: rowData.HSXMLXBH,
                            },
                            okCallback: async () => {
                                if (rowData.HSXMLXBH) {
                                    // 删除 辅助账
                                    const r = await AssistDel({
                                        dbname: diaParams.current?.dbname,
                                        Data_ID: diaParams.current?.Data_ID,
                                        editUser: diaParams.current?.editUser,
                                        KMBH: rowData.KMBH,
                                        HSXMLXBH: rowData.HSXMLXBH,
                                        HSXMBH: rowData.code + "~|",
                                    });
                                    if (!r.ok) {
                                        notify(`${r.data}`, "warning", 2000);
                                    }
                                    else {
                                        notify("删除辅助账成功", "success", 2000);
                                        getSubjectSDBDatas(CheckBoxVaue);
                                    }
                                }
                                else {
                                    // 删除 科目表
                                    const r = await SubjectDel({
                                        dbname: diaParams.current?.dbname,
                                        Data_ID: diaParams.current?.Data_ID,
                                        editUser: diaParams.current?.editUser,
                                        KMBH: rowData.KMBH,
                                    });
                                    if (r.ok) {
                                        notify("删除科目成功", "success", 2000);
                                        getSubjectSDBDatas(CheckBoxVaue);
                                    }
                                    else {
                                        notify(`${r.data}`, "warning", 2000);
                                    }
                                }
                            },
                            cancelCallback: () => { },
                        });
                    },
                },
            ];
            e.items.push(...tempMenus);
        }
    };
    function okCallbackData(okRowData) {
        if (!okRowData.itemNames3)
            return notify("此科目未对应报表项目，请先对科目进行对应后，再进行调整", "error", 2000);
        diaParams.current?.okCallback(okRowData);
        setValue("");
        setTbsStates("0");
        setVisible(false);
        setCheckBoxVaue(true);
        TreeListRef.current.instance.searchByText();
        TreeListRef.current.instance.clearSelection();
    }
    const onCellClick = (r) => {
        diaParamsData.current = r.data;
    };
    const onCellDblClick = async (r) => {
        // 科目 判断 是否为末级
        diaParamsData.current = r.data;
        if (diaParams.current?.isSelectSuperior) {
            okCallbackData(r.data);
        }
        else {
            if (!r.data?.ID)
                return;
            if (r.data?.isEnd) {
                okCallbackData(r.data);
            }
            else {
                notify("非末级结点科目,无法选中!", "error", 2000);
            }
        }
    };
    const okPopup = async () => {
        if (diaParams.current?.isSelectSuperior) {
            okCallbackData(diaParamsData.current);
        }
        else {
            if (!diaParamsData.current?.ID)
                return;
            if (diaParamsData.current?.isEnd) {
                okCallbackData(diaParamsData.current);
            }
            else {
                notify("非末级结点科目,无法选中!", "error", 2000);
            }
        }
    };
    const canclePopup = async () => {
        setValue("");
        setTbsStates("0");
        setCheckBoxVaue(true);
        TreeListRef.current.instance.searchByText();
        TreeListRef.current.instance.clearSelection();
        setVisible(false);
    };
    return (_jsxs("div", { style: { width: "100%", height: "100%" }, children: [_jsx(DevPopup.Logic, { ref: ref, width: 1000, height: 700, visible: visible, title: "\u9009\u62E9\u79D1\u76EE", onOK: okPopup, onCancel: canclePopup, onHiding: canclePopup, children: () => {
                    return (_jsxs("div", { style: { width: "100%", height: "100%", position: "relative" }, children: [openVisible111111 && (_jsx("div", { style: {
                                    position: "absolute",
                                    width: "100%",
                                    height: "100%",
                                    display: "flex",
                                    alignItems: "center",
                                    justifyContent: "center",
                                    opacity: 0.5,
                                    zIndex: "99999",
                                    background: "#ddd",
                                }, children: _jsx(LoadIndicator, { id: "medium-indicator", height: 50, width: 50 }) })), _jsxs("div", { style: { display: "flex", justifyContent: "space-between" }, children: [_jsxs("div", { style: {
                                            display: "flex",
                                            alignItems: "center",
                                            marginBottom: "10px",
                                            marginLeft: "-9px",
                                        }, children: [_jsxs("span", { style: {
                                                    display: "flex",
                                                    alignItems: "center",
                                                    justifyContent: "center",
                                                    marginLeft: "11px",
                                                }, children: [_jsx(CheckBox, { value: !CheckBoxVaue, onValueChanged: (e) => {
                                                            getSubjectSDBDatas(!e.value);
                                                            setCheckBoxVaue(!e.value);
                                                            setTbsStates("0");
                                                        } }), _jsx("span", { style: { marginLeft: "8px" }, children: "\u4EC5\u663E\u793A\u603B\u8D26" })] }), _jsx("span", { style: { color: "red", marginLeft: "20px" }, children: "\u63D0\u793A\uFF1A\u53CC\u51FB\u53EF\u9009\u4E2D\u76F8\u5E94\u79D1\u76EE" })] }), _jsxs("div", { style: {
                                            display: "flex",
                                            alignItems: "center",
                                            marginBottom: "10px",
                                            marginLeft: "20px",
                                        }, children: [_jsx("span", { style: { marginRight: "15px" }, children: _jsx(TextBox, { width: 200, onInput: (e) => getInputValue(e), value: value, className: "TextBoxButtons_Style", buttons: [
                                                        {
                                                            location: "before",
                                                            name: "search",
                                                            options: {
                                                                icon: "iconfont icon-search",
                                                                disabled: true,
                                                            },
                                                        },
                                                    ] }) }), _jsx("span", { className: "refresh_Style", children: _jsx(CpasIcon, { fontSize: 15, type: "icon-refresh", onClick: () => {
                                                        getSubjectSDBDatas(CheckBoxVaue);
                                                        getSubjectTypeData();
                                                        setTbsStates("0");
                                                    } }) })] })] }), _jsxs("div", { style: { display: "flex", height: "93%" }, children: [_jsx("div", { style: { border: "1px solid #ddd", borderRight: "none" }, children: _jsx(HocVerticalLeftStyleTab, { activeKey: TbsStates, className: "AccountSubjectsTabs", tabPosition: "left", tabBarStyle: { background: "#FFF" }, onChange: AccountTabsClick, children: Array.isArray(tabsData)
                                                ? tabsData.map((item) => {
                                                    return (_jsx(AntTabs.Panel, { tab: item.type, style: { marginTop: "5px" } }, item.code));
                                                })
                                                : "" }) }), _jsxs(TreeList, { selection: {
                                            recursive: true,
                                            mode: selectMode === "0" ? "single" : "multiple",
                                        }, className: "PublicTreeList devHeadersCenter", ref: TreeListRef, dataSource: SubjectSDBDatas, allowColumnResizing: true, columnAutoWidth: true, showRowLines: true, showBorders: true, columnResizingMode: "widget", columnMinWidth: 100, rootValue: "0", parentIdExpr: "PID", keyExpr: "ID", id: "Code", width: "95%", onCellClick: onCellClick, onCellDblClick: onCellDblClick, onContextMenuPreparing: relationMenuItems, 
                                        // 显示斑马线
                                        rowAlternationEnabled: true, children: [_jsx(Column, { dataField: "code", caption: "\u79D1\u76EE\u7F16\u53F7", width: 130, alignment: "left" }), _jsx(Column, { dataField: "displayName", caption: "\u79D1\u76EE\u540D\u79F0", width: 200, alignment: "left" }), _jsx(Column, { dataField: "KMFX", caption: "\u79D1\u76EE\u65B9\u5411", width: 80, alignment: "center" }), _jsx(Column, { dataField: "QCYE", caption: "\u671F\u521D\u4F59\u989D", width: 120, alignment: "right", cellRender: (cell) => {
                                                    return cell.data.QCYE && cell.data.QCYE !== 0
                                                        ? moneyFormat(cell.data.QCYE)
                                                        : " ";
                                                } }), _jsx(Column, { dataField: "JFLJFSE", caption: "\u501F\u65B9\u7D2F\u8BA1", width: 130, alignment: "right", cellRender: (cell) => {
                                                    return cell.data.JFLJFSE !== 0
                                                        ? cell.data.JFLJFSE !== null
                                                            ? moneyFormat(cell.data.JFLJFSE)
                                                            : ""
                                                        : "";
                                                } }), _jsx(Column, { dataField: "DFLJFSE", caption: "\u8D37\u65B9\u7D2F\u8BA1", width: 130, alignment: "right", cellRender: (cell) => {
                                                    return cell.data.DFLJFSE && cell.data.DFLJFSE !== 0
                                                        ? moneyFormat(cell.data.DFLJFSE)
                                                        : " ";
                                                } }), _jsx(Column, { dataField: "QMYE", caption: "\u671F\u672B\u4F59\u989D", width: 130, alignment: "right", cellRender: (cell) => {
                                                    return cell.data.QMYE && cell.data.QMYE !== 0
                                                        ? moneyFormat(cell.data.QMYE)
                                                        : " ";
                                                } }), _jsx(Column, { dataField: "itemNames2", caption: "\u62A5\u8868\u9879\u76EE", width: 130, alignment: "left" })] }, treeListKey)] })] }));
                } }), _jsx(DevPopup.Default, { ref: typeRef, children: () => {
                    return (_jsx("span", { children: `【${curRowKMMC}】为末级科目，是否继续为其增加下级科目？` }));
                } }), _jsx(DevPopup.Default, { ref: AuxiliaryRef, children: () => {
                    //
                    return _jsx("span", { children: AddFZZMessage });
                } }), _jsx(DevPopup.Default, { ref: typeErrorRef, children: () => {
                    return (_jsxs(_Fragment, { children: [_jsx("span", { children: `【${curRowKMMC}】` }), _jsx("span", { style: { color: "red" }, children: "\u4E3A\u672B\u7EA7\u79D1\u76EE\uFF0C\u5DF2\u88AB\u8BBE\u7F6E\u8F85\u52A9\u8D26\uFF0C\u65E0\u6CD5\u589E\u52A0\u4E0B\u7EA7\u79D1\u76EE!" })] }));
                } }), _jsx(DevPopup.Default, { ref: AuxiliaryRefs, children: () => {
                    //
                    return _jsx("span", { children: `科目【${curRowKMMC}】为非末级，无法增加辅助帐` });
                } }), _jsx(DevPopup.Default, { ref: DeleteSubjectRef, children: () => {
                    return (_jsxs("div", { children: [_jsx("p", { children: "\u786E\u5B9A\u8981\u5220\u9664:" }), curNodeRef.current?.HSXMMC ? (_jsxs("p", { children: ["\u3010\u8F85\u52A9\u8D26\u3011", curNodeRef.current?.HSXMMC] })) : (_jsx("p", { children: curNodeRef.current?.KMMC }))] }));
                } }), _jsx(SubjectsPopupData, { ref: SubjectPopupRef, AccountDataID: diaParams.current?.Data_ID, dbname: diaParams.current?.dbname, username: diaParams.current?.editUser }), _jsx(AuxiliaryPopup, { ref: AuxiliaryPopupRef, AccountDataID: diaParams.current?.Data_ID, dbname: diaParams.current?.dbname, username: diaParams.current?.editUser }), _jsx(DevPopup.Default, { ref: TreeAuxiliaryRef, children: () => {
                    //
                    return _jsx("span", { children: `${AddFZZKMMC}` });
                } })] }));
});
export default React.memo(CarryEntryPopup);
